﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BOM.entity;
using BOM.dao;
using System.Collections;
using System.Text.RegularExpressions;

namespace BOM
{
    public partial class BomForm : Form
    {
        private Bom editData;
        private int oprtState = 0; //默认查看
        private TreeNode parentNode; //父节点
        private Bom parentBom;//父BOM
        

        public BomForm(Hashtable ctx)
        {
            InitializeComponent();

            if (ctx["curnode"] != null)
            {
                parentNode = (TreeNode)ctx["curnode"];
                parentBom = (Bom)parentNode.Tag;
            }
            oprtState = (int)ctx["oprtstate"];
            if (oprtState == 1)
            {
                editData = new Bom();
                txtNaterialCost.ReadOnly = false;
                txtLabourCost.ReadOnly = false;
            }
            else if (oprtState == 2 || oprtState == 0)
            {
                int id = (int)ctx["id"];
                editData = BomDaoImp.getBomById(id);
                loadFields();
            }
            setComponentStatus(oprtState);
        }
        //保存按钮
        private void btnSave_Click(object sender, EventArgs e)
        {
            storeFileds();
            if (!verifyInput())
                return;
            int n =0;
            if (oprtState == 1)
            {
                n = BomDaoImp.addNew(editData);
            }
            else
            {
                n = BomDaoImp.update(editData);
               
            }
            if (n > 0)
            {
                updateCost(editData); //更新成本字段
                MessageBox.Show(this,"保存成功!","提示");
                this.Close();
            }
            else
                MessageBox.Show(this,"保存失败!","提示");
        }
        //校验输入
        public Boolean verifyInput()
        {
            if (editData.getPartNumber() == null || "".Equals(editData.getPartNumber()))
            {
                MessageBox.Show("PartNumber不能为空！", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (editData.getPartName() == null || "".Equals(editData.getPartName()))
            {
                MessageBox.Show("PartName不能为空！", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }
        //删除按钮
        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show(this,"是否确认要删除该BOM信息","提示",MessageBoxButtons.YesNo, MessageBoxIcon.Question,MessageBoxDefaultButton.Button1);
            if (result == DialogResult.Yes)
            {
                Bom parentBom = BomDaoImp.getBomById(editData.getParentId());
                BomDaoImp.remove(editData);
                updateCost(parentBom); //更新成本字段
                MessageBox.Show(this,"删除成功！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                this.Close();
            }
        }
        //更新成本
        private void btnUpdateCost_Click(object sender, EventArgs e)
        {
            if (editData.getType()!=2)
            {
                MessageBox.Show(this,"当前编辑的BOM表是组件类型，不能直接编辑成本值！","提示");
                return;
            }
            CheckAdmin checkAdmin = new CheckAdmin();
            DialogResult result = checkAdmin.ShowDialog(this);
            if (result == DialogResult.OK)
            {
                txtNaterialCost.ReadOnly = false;
                txtLabourCost.ReadOnly = false;
            }
        }
        //保存数据至对象
        public void storeFileds()
        {
            if (editData == null)
                editData = new Bom();
            if (oprtState == 1)
            {
                editData.setParentId(parentBom.getId());
                editData.setLevel(parentBom.getLevel() + 1);
            }
            editData.setLongNumber(parentBom.getLongNumber()+"!"+txtPartNumber.Text.Trim());
            editData.setPartName(txtPartName.Text.Trim());
            editData.setPartNumber(txtPartNumber.Text.Trim());
            editData.setDarawingNo(txtDarawingNo.Text.Trim());
            if (txtNaterialCost.Text != null && !"".Equals(txtNaterialCost.Text))
            editData.setNaterialCost(float.Parse(txtNaterialCost.Text.Trim()));
            if (this.txtLabourCost.Text != null && !"".Equals(txtLabourCost.Text))
            editData.setLabourCost(float.Parse(txtLabourCost.Text.Trim()));
            if (this.txtTotalCost.Text != null && !"".Equals(txtTotalCost.Text))
            editData.setTotalCost(editData.getNaterialCost()+editData.getLabourCost());
            editData.setDesigner(this.txtDesigner.Text.Trim());
            editData.setVersion(this.txtVersion.Text.Trim());
            editData.setCompenent(this.txtComponent.Text.Trim());
            editData.setMaterial(this.txtMaterial.Text.Trim());
            editData.setSupplier1(this.txtSupplier1.Text.Trim());
            editData.setSupplier2(this.txtSupplier2.Text.Trim());
            editData.setSupplyCycle(this.txtSupplyCycle.Text.Trim());
            editData.setIsLeaf(this.chkIsLeaf.Checked);
            editData.setType(this.getTypeValue());
            int qty = 1;
            if (this.txtQty.Text == null || "".Equals(txtQty.Text))
                qty = 1;
            else
                qty = int.Parse(this.txtQty.Text.Trim());
            editData.setQty(qty);
            editData.setSumCost(qty * editData.getTotalCost());

        }
        //从数据对象加载数据到界面
        public void loadFields()
        {
            if (editData == null)
                return;
            txtPartNumber.Text = editData.getPartNumber();
            txtPartName.Text = editData.getPartName();
            txtDarawingNo.Text = editData.getDarawingNo();
            txtNaterialCost.Text =editData.getNaterialCost().ToString();
            txtLabourCost.Text =  editData.getLabourCost().ToString();
            txtTotalCost.Text = editData.getTotalCost().ToString();
            txtDesigner.Text = editData.getDesigner();
            txtVersion.Text = editData.getVersion();
            txtComponent.Text = editData.getComponent();
            txtMaterial.Text = editData.getMaterial();
            txtSupplier1.Text = editData.getSupplier1();
            txtSupplier2.Text = editData.getSupplier2();
            txtSupplyCycle.Text = editData.getSupplyCycle();
            chkIsLeaf.Checked = editData.isleaf();
            if (editData.getType() != null)
                resetTypeStatus(editData.getType());
            txtQty.Text = editData.getQty().ToString();
            txtSumCost.Text = editData.getSumCost().ToString();
        }
        //退出按钮
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //更新成本
        public static void updateCost(Bom bom)
        {
            BomDaoImp.updateCost(bom);
            if (bom.getParentId() == null || bom.getParentId()==0)
                return;
            else
            {
                Bom parentBom = BomDaoImp.getBomById(bom.getParentId());
                updateCost(parentBom);
            }
        }

        //限制只可以输入数字
        private void numberAccpter(object sender, KeyPressEventArgs e)
        {
            int keyValue = (int)e.KeyChar;
            if ((keyValue >= 48 && keyValue <= 57) || keyValue == 8 || keyValue == 46)
            {
                if (sender != null && sender is TextBox && keyValue == 46)
                {
                    if (((TextBox)sender).Text.IndexOf(".") >= 0)
                        e.Handled = true;
                    else
                        e.Handled = false;
                }
                else
                    e.Handled = false;
            }
            else
                e.Handled = true;
        }
        
        private void txtNaterialCost_KeyPress(object sender, KeyPressEventArgs e)
        {
            numberAccpter(sender,e);
        }

        private void txtLabourCost_KeyPress(object sender, KeyPressEventArgs e)
        {
            numberAccpter(sender, e);
        }

        private void txtNaterialCost_Validated(object sender, EventArgs e)
        {
            resetTotalCost();
        }

        private void txtLabourCost_Validated(object sender, EventArgs e)
        {
            resetTotalCost();
        }
        //重新计算总成本
        public void resetTotalCost(){
            float naterialCost = 0;
            float labourCost = 0;
            if (!"".Equals(txtNaterialCost.Text.Trim()))
                naterialCost =float.Parse(txtNaterialCost.Text.Trim());
            if (!"".Equals(txtLabourCost.Text.Trim()))
                labourCost = float.Parse(txtLabourCost.Text.Trim());
            float totalCost = naterialCost + labourCost ;
            txtTotalCost.Text = totalCost.ToString();
            int qty = 0;
            if (!"".Equals(txtQty.Text))
                qty = int.Parse(txtQty.Text.Trim());
            float sumCost = totalCost * qty;
            txtSumCost.Text = sumCost.ToString();
        }
        //退出按钮
        private void btnEdit_Click(object sender, EventArgs e)
        {
            oprtState = 2;
            setComponentStatus(oprtState); 
        }
        //初始化控件状态 0 查看，1 新增，2 修改
        public void setComponentStatus(int oprtState)
        {
            if (0 == oprtState)
            {
                btnSave.Enabled = false;
               // btnDelete.Enabled = false;
                btnUpdateCost.Enabled = false;
                txtPartNumber.Enabled = false;
                txtPartName.Enabled = false;
                txtDarawingNo.Enabled = false;
                txtDesigner.Enabled = false;
                txtVersion.Enabled = false;
                txtComponent.Enabled = false;
                txtMaterial.Enabled = false;
                txtSupplier1.Enabled = false;
                txtSupplier2.Enabled = false;
                txtSupplyCycle.Enabled = false;
                chkIsLeaf.Enabled = false;
                txtQty.Enabled = false;
            }
            else if (1 == oprtState)
            {
                rComponent.Select();
            }
            else if (2 == oprtState)
            {
                btnSave.Enabled = true;
                btnDelete.Enabled = true;
                btnUpdateCost.Enabled = true;
                txtPartNumber.Enabled = true;
                txtPartName.Enabled = true;
                txtDarawingNo.Enabled = true;
                txtDesigner.Enabled = true;
                txtVersion.Enabled = true;
                txtComponent.Enabled = true;
                txtMaterial.Enabled = true;
                txtSupplier1.Enabled = true;
                txtSupplier2.Enabled = true;
                txtSupplyCycle.Enabled = true;
                chkIsLeaf.Enabled = true;
                txtQty.Enabled = true;
            }
        }

        public int getTypeValue()
        {
            if (rComponent.Checked)
                return 1;
            else if (rPart.Checked)
                return 2;
            else  
                return 3;
        }

        public void resetTypeStatus(int type){
            if (1 == type)
            {
                rComponent.Checked = true;
                rPart.Checked = false;
                rDoc.Checked = false;
            }
            else if(2 == type)
            {
                rPart.Checked = true;
                rComponent.Checked = false;
                rDoc.Checked = false;
            }
            else if(3 == type)
            {
                rDoc.Checked = true;
                rComponent.Checked = false;
                rPart.Checked = false;
            }
        }
        
        private void txtQty_Validated(object sender, EventArgs e)
        {
            resetTotalCost();
        }

        private void txtSumCost_Validated(object sender, EventArgs e)
        {

        }

        private void txtQty_KeyPress(object sender, KeyPressEventArgs e)
        {

        }
        //控制回车跳转
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                foreach (Control c in this.Controls)
                {
                    if (c is System.Windows.Forms.TextBox)
                    {
                        keyData = Keys.Tab;
                    }
                }
                keyData = Keys.Tab;
            }
            return base.ProcessDialogKey(keyData);
        }

        private void rComponent_CheckedChanged(object sender, EventArgs e)
        {
            resetCostFieldStatusByType();
        }

        private void rPart_CheckedChanged(object sender, EventArgs e)
        {
            resetCostFieldStatusByType();
        }

        private void rDoc_CheckedChanged(object sender, EventArgs e)
        {
            resetCostFieldStatusByType();
        }

        public void resetCostFieldStatusByType()
        {
            int type = getTypeValue();
            //组件和文档不可编辑成本字段
            if (1 == type || 3 == type)
            {
                txtNaterialCost.Enabled = false;
                txtLabourCost.Enabled = false;
                txtQty.Enabled = false;
            }
            //零件可编辑成本字段
            else
            {
                txtNaterialCost.Enabled = true;
                txtLabourCost.Enabled = true;
                txtQty.Enabled = true;
            }
        }
    }
}
